package com.hq.ims.home.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entity.DataStaging;
import com.hq.ims.data.service.BaseDataStagingService;
import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.utils.RedisUtil;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.exception.CommonException;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.utils.vo.ReturnVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 * @ClassName DataStagingService
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class DataStagingBusService {

    @Resource
    private BaseDataStagingService baseDataStagingService;


    /**
     * 添加
     *
     * @param key   关键
     * @param value 价值
     * @return {@code ReturnVo<Boolean> }
     */
    public ReturnVo<Boolean> add(String key, Object value) {
        //当前用户等信息
        String curUserId = AppUtils.getUserId();
        if (StringUtils.isEmpty(curUserId)) {
            throw CommonException.build(ErrMsgConstant.USER_INFO_NOT_EXISTS);
        }
        String companyId = WebExpandUtils.getHeadCompanyId();
        String projectId = WebExpandUtils.getHeadProjectId();
        //获取完整key
        String fullKey = RedisUtil.getSingleTimeCacheKey(AppUtils.getUserId(), key);

        //创建数据暂存对象
        DataStaging dataStaging = new DataStaging();
        dataStaging.setUserId(curUserId);
        dataStaging.setCompanyId(companyId);
        dataStaging.setProjectId(projectId);
        dataStaging.setKey(key);
        dataStaging.setFullKey(fullKey);
        String valueStr = JsonUtils.objectToString(value);
        dataStaging.setValue(valueStr);
        dataStaging.setIsUsed(false);

        //保存
        dataStaging.insert();

        return ReturnVo.buildSuccess(true);
    }

    /**
     * 获取当前用户数据暂存分页列表
     *
     * @param requestPage 请求页面
     * @param dataStaging 数据暂存
     * @return {@code ReturnVo<ReturnPage<DataStaging>> }
     */
    public ReturnVo<ReturnPage<DataStaging>> getStagingByPage(RequestPage requestPage, DataStaging dataStaging) {
        //当前用户等信息
        String curUserId = AppUtils.getUserId();
        if (StringUtils.isEmpty(curUserId)) {
            throw CommonException.build(ErrMsgConstant.USER_INFO_NOT_EXISTS);
        }

        //获取当前用户数据暂存分页列表
        LambdaQueryChainWrapper<DataStaging> lq = baseDataStagingService.lambdaQuery()
                .eq(DataStaging::getIsUsed, false)
                .eq(DataStaging::getUserId, curUserId);

        lq.eq(StringUtils.isNotEmpty(dataStaging.getKey()), DataStaging::getKey, dataStaging.getKey());
        lq.eq(StringUtils.isNotEmpty(dataStaging.getCompanyId()), DataStaging::getCompanyId, dataStaging.getCompanyId());
        lq.eq(StringUtils.isNotEmpty(dataStaging.getProjectId()), DataStaging::getProjectId, dataStaging.getProjectId());
        lq.orderByDesc(DataStaging::getCreateDateTime)
                .page(requestPage);

        return ReturnVo.buildSuccess(requestPage.getReturnPage());
    }

    /**
     * 获取数据暂存详情
     *
     * @param stagingId 临时身份证
     * @return {@code ReturnVo<Object> }
     */
    public ReturnVo<Object> getStagingById(String stagingId) {
        DataStaging dataStaging = baseDataStagingService.getById(stagingId);
        ObjectUtils.isNullThrow(dataStaging, "数据暂存数据为空");
        return ReturnVo.buildSuccess(JsonUtils.stringToObject(dataStaging.getValue(), Object.class));
    }

    /**
     * 删除暂存
     *
     * @param stagingId 临时身份证
     * @return {@code ReturnVo<Boolean> }
     */
    public ReturnVo<Boolean> deleteStaging(String stagingId) {
        DataStaging dataStaging = baseDataStagingService.getById(stagingId);
        ObjectUtils.isNullThrow(dataStaging, "数据暂存数据为空");

        if (dataStaging.getIsUsed()) {
            throw CommonException.build("数据暂存已被使用，无法删除");
        }

        dataStaging.deleteById();
        return ReturnVo.buildSuccess(true);
    }

    /**
     * 使用数据暂存
     *
     * @param stagingId 临时身份证
     * @return {@code ReturnVo<Boolean> }
     */
    public ReturnVo<Boolean> userStaging(String stagingId) {
        DataStaging dataStaging = baseDataStagingService.getById(stagingId);
        ObjectUtils.isNullThrow(dataStaging, "数据暂存数据为空");

        if (dataStaging.getIsUsed()) {
            throw CommonException.build("数据暂存已被使用，无法再次使用");
        }

        dataStaging.setIsUsed(true);
        dataStaging.updateById();
        return ReturnVo.buildSuccess(true);
    }

}